<?php
    namespace yaliSDK\utils;


    class WXHelper {
        // 根据微信appid获取配置信息
        static function getWXSetByAppId($appId) {
            $all =config('appid_list');
            if ($appId && !empty($all[$appId])) {
                return $all[$appId];
            }
            return null;
        }
        static function getLiveWXSet() {
            return [
                // 微信小程序 App ID
                'WX_APPID' => 'wx572342454110544e',
                // 微信小程序 App Secret
                'WX_APPSECRET' => '30ebed63d39fee48dac829c962bf8162',
            ];
        }
        /**
         * 向微信后台请求 获取openid。
         */
        static function getWxInfoByCode($code, $config=null) {
            if (!empty($config)) {
                $data = [
                    'appid' => $config['WX_APPID'] . '',
                    'secret' => $config['WX_APPSECRET'],
                    'js_code' => $code,
                    'grant_type' => 'authorization_code',
                ];
                $getUrl = 'https://api.weixin.qq.com/sns/jscode2session?' . http_build_query($data);
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $getUrl);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//            curl_setopt($ch, CURL_SSLVERSION_SSL, 2);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
                $data = curl_exec($ch);
                return json_decode($data);
            }
            return null;
        }
    }